<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/20
 * Time: 15:11
 */
require('../lib/db.php' );
$action = $_GET['action'];
switch($action){
    case "add":
        add();
        break;
    case "del":
        del();
        break;
    case "edit":
        edit();
        break;
}

function add(){
    //生成会员编号
    open_mysql_connect();
    $today = strtotime('today');
    $sql = "select count(*) as num from mf_customer where cus_addtime>=$today";
    $res = mysql_query($sql);
    $row = mysql_fetch_assoc($res);
    $num = $row['num']+1;
    if($num<10){
        $num = '000'.(string)($num);
    }
    if($num<100 && $num>=10){
        $num = '00'.(string)($num);
    }
    if($num<1000 && $num>=100){
        $num = '0'.(string)($num);
    }
    $cus_vip_number ='MF'.date('Ymd',time()).$num;
    $infos = $_POST['infos'];
    parse_str($infos,$data);
   foreach($data as $k=>$v){
        $$k = $v;
       //判断是否有空值
       /*if($v==''){
          return false;exit;
       }*/
    }
    $cus_number =addslashes($cus_number);
    $cus_fname = addslashes($cus_fname);
    $cus_aname = addslashes($cus_aname);
    $cus_cate = addslashes($cus_cate);
    $cus_area = addslashes($cus_area);
    $cus_address = addslashes($cus_address);
    $cus_linkman = addslashes($cus_linkman);
    $cus_phone = ($cus_phone)+0;
    $cus_identity_num = addslashes($cus_identity_num);
    $cus_iscopy = (int)($cus_iscopy);
    $cus_birthday = addslashes($cus_birthday);
    $cus_email = addslashes($cus_email);
    $cus_tel = (int)($cus_tel);
    $cus_fax = (int)($cus_fax);
    $cus_sex = (int)$cus_sex;
    if(!isset($_POST['cus_cert'])){
        $cus_cert = '无';
    }else{
        $cus_cert = implode(',',$cus_cert);
    }
    $cus_isget = (int)($cus_isget);
    $cus_business_p = addslashes($cus_business_p);
    $cus_business_m = addslashes($cus_business_m);
    $cus_service = addslashes($cus_service);
    $cus_remark = addslashes($cus_remark);
    $cus_addtime = time();
    $province = $data['s_province'] .','. $data['s_city'] .','. $data['s_county'];
    $cus_province = addslashes($province);
    $sql ="insert into mf_customer(cus_number,cus_fname,cus_aname,cus_cate,cus_area,cus_province,cus_address,cus_linkman,cus_phone,cus_identity_num,cus_iscopy,cus_birthday,cus_email,cus_tel,cus_fax,cus_sex,cus_cert,cus_isget,cus_business_p,cus_business_m,cus_service,cus_remark,cus_addtime,cus_vip_number) values
          ('$cus_number','$cus_fname','$cus_aname','$cus_cate','$cus_area','$cus_province','$cus_address','$cus_linkman','$cus_phone','$cus_identity_num','$cus_iscopy','$cus_birthday','$cus_email',$cus_tel,'$cus_fax',$cus_sex,'$cus_cert',$cus_isget,'$cus_business_p','$cus_business_m','$cus_service','$cus_remark',$cus_addtime,'$cus_vip_number')";
    $res = db_exec_update($sql);
    echo $res;
}

function edit(){
    $infos = $_POST['infos'];
    parse_str($infos,$data);
    foreach($data as $k=>$v){
        $$k = $v;
    }
    if($data['s_province']=='省份' || $data['s_city'] == '地级市' || $data['s_county']=='县级市'){
        $cus_s_province = addslashes($data['cus_province']);
    }else{
        $cus_s_province = addslashes($data['s_province']) .','. addslashes($data['s_city']) .','. addslashes($data['s_county']);
    }
    $cus_id = $_GET['id'];
    //接收传值
    $cus_number =addslashes($cus_number);
    $cus_fname = addslashes($cus_fname);
    $cus_aname = addslashes($cus_aname);
    $cus_cate = addslashes($cus_cate);
    $cus_area = addslashes($cus_area);
    $cus_address = addslashes($cus_address);
    $cus_linkman = addslashes($cus_linkman);
    $cus_phone = $cus_phone+0;
    $cus_identity_num = addslashes($cus_identity_num);
    $cus_iscopy = (int)($cus_iscopy);
    $cus_birthday = addslashes($cus_birthday);
    $cus_email = addslashes($cus_email);
    $cus_tel = (int)($cus_tel);
    $cus_fax = (int)($cus_fax);
    $cus_sex = (int)$cus_sex;
    $cus_cert = implode(',',$cus_cert);
    $cus_isget = (int)($cus_isget);
    $cus_business_p = addslashes($cus_business_p);
    $cus_business_m = addslashes($cus_business_m);
    $cus_service = addslashes($cus_service);
    $cus_remark = addslashes($cus_remark);
    $sql = "update mf_customer set
          cus_number='$cus_number',
          cus_fname='$cus_fname',
          cus_aname='$cus_aname',
          cus_cate='$cus_cate',
          cus_area='$cus_area',
          cus_address='$cus_address',
          cus_linkman='$cus_linkman',
          cus_phone='$cus_phone',
          cus_identity_num='$cus_identity_num',
          cus_iscopy='$cus_iscopy',
          cus_birthday='$cus_birthday',
          cus_email='$cus_email',
          cus_tel='$cus_tel',
          cus_fax='$cus_fax',
          cus_sex='$cus_sex',
          cus_cert='$cus_cert',
          cus_isget='$cus_isget',
          cus_business_p='$cus_business_p',
          cus_business_m='$cus_business_m',
          cus_service='$cus_service',
          cus_remark='$cus_remark',
          cus_province='$cus_s_province'
          where cus_id=($cus_id)";
    $res=db_exec_update($sql);

    echo $res;
}

function del(){
    $ids = $_POST['ids'];
    $sql = "delete from mf_customer where cus_id in ($ids)";
    $res = db_exec_update($sql);
    echo $res;
}
